package com.john.utils.common;

import com.john.utils.enums.CharsetName;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date ：Created in 4:49 PM 2019/9/26
 *
 *  *
 *  * 测试方式:
 *  *
 * */


 
public class CmdShellUtils {
 
    public static void main(String[] args) {
        /* List<String> commend = new ArrayList<>();
        commend.add("java");
        commend.add("-version");
        Process process = CmdShellUtils.exec(commend);
        String message = CmdShellUtils.getOutput(process);
        CmdShellUtils.destroy(process);
        System.out.println(message); */

        String commends = "ipconfig";
        Process process = CmdShellUtils.exec(commends);
        String message = CmdShellUtils.getOutput(process);
        CmdShellUtils.destroy(process);
        System.out.println(message);
    }

    /**
     * 执行脚本命令
     * @param commend
     * @throws
     */
    public static Process exec(String commend) {
        try {
            ProcessBuilder processBuilder = new ProcessBuilder();
            //设置管理员权限
            processBuilder.redirectErrorStream(true);
            processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);

            if(System.getProperty("os.name").startsWith("Windows")){
                processBuilder.command("runas");
            }else{
                processBuilder.command("sudo");
            }

            processBuilder.command("cmd","/c",commend);

            Process process = processBuilder.start();

            int exitCode = process.waitFor();

            System.out.println(StreamUtils.InputStreamToString(process.getInputStream(), CharsetName.CHAR_GBK));
            System.out.println("==================================");
            System.out.println(StreamUtils.InputStreamToString(process.getErrorStream(), CharsetName.CHAR_GBK));
            System.out.println("exit code:" + exitCode);

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 执行脚本命令
     * @param commend
     * @throws
     */
    public static Process exec(List<String> commend) {
        Process process = null;
        try {
            String[] commends = new String[commend.size()];
            commend.toArray(commends);
            process = new ProcessBuilder().command(commends).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return process;
    }
 
    /**
     *
     * @param process
     * @return  java.lang.String
     * @date    2019/9/26
     * @throws
     */
    public static String getOutput(Process process) {
        System.out.println(StreamUtils.InputStreamToString(process.getInputStream(), CharsetName.CHAR_GBK));
        System.out.println(StreamUtils.InputStreamToString(process.getErrorStream(), CharsetName.CHAR_GBK));
        /* String output = null;
        BufferedReader reader = null;
        try {
            if (process != null) {
                StringBuffer stringBuffer = new StringBuffer();
                reader = new BufferedReader(new InputStreamReader(new SequenceInputStream(process.getInputStream(), process.getErrorStream())));
                while (reader.read() != -1){
                    stringBuffer.append("\n" + reader.readLine());
                }
                output = stringBuffer.toString();
            }
            closeQuietly(reader);
        } catch (Exception e) {
            output = null;
            e.printStackTrace();
        } */
        return null;
    }

    /**
     *连续读取Process数据
     * @param process
     * @return  java.lang.String
     * @date    2019/9/26
     * @throws
     */
    public static boolean getOutputCallback(Process process,CmdShellMessageListener cmdShellMessageListener) {
        BufferedReader reader = null;
        try {
            if (process != null) {
                reader = new BufferedReader(new InputStreamReader(new SequenceInputStream(process.getInputStream(), process.getErrorStream())));
                String line;
                while ((line = reader.readLine()) != null) {
//                    System.out.println("screen-投屏 line = " + line);
                    cmdShellMessageListener.messageCallback(line);
                }
            }
            closeQuietly(reader);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 关流
     * @param reader
     * @return  void
     * @date    2019/9/26
     * @throws
     */
    public static void closeQuietly(Reader reader) {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
 
    /**
     * 终止进程
     * @param process
     * @return  void
     * @date    2019/9/26
     * @throws
     */
    public static void destroy(Process process) {
        if (process != null) {
            process.destroyForcibly();
        }
    }

    /**
     * 读取信息回调
     */
    public interface CmdShellMessageListener {
        void messageCallback(String message);
    }

}